﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace X.Common
{
    public static class StringUtils
    {
        public static bool IsIncludeChinese(string input)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]");
            return regex.Match(input).Success;
        }

        public static string FilterHtml(string htmlString)
        {
            //删除脚本
            htmlString = Regex.Replace(htmlString, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML
            htmlString = Regex.Replace(htmlString, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"-->", "", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"<!--.*", "", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&#(\d+);", "", RegexOptions.IgnoreCase);

            htmlString = Regex.Replace(htmlString, @"&ldquo;", "“", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&rdquo;", "”", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&lsquo;", "‘", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&rsquo;", "’", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&mdash;", "—", RegexOptions.IgnoreCase);

            htmlString = Regex.Replace(htmlString, @"&middot;", "·", RegexOptions.IgnoreCase);

            htmlString.Replace("<", "");
            htmlString.Replace(">", "");
            htmlString.Replace("\r\n", "");
            htmlString = HtmlEncode(htmlString).Trim();

            return htmlString;
        }

        public static string SubString(string demand, int length, string substitute)
        {
            demand = HtmlDecode(demand);
            if (Encoding.Default.GetBytes(demand).Length <= length)
            {
                return demand;
            }
            ASCIIEncoding encoding = new ASCIIEncoding();
            length -= Encoding.Default.GetBytes(substitute).Length;
            int num = 0;
            StringBuilder builder = new StringBuilder();
            byte[] bytes = encoding.GetBytes(demand);
            for (int i = 0; i < bytes.Length; i++)
            {
                if (bytes[i] == 0x3f)
                {
                    num += 2;
                }
                else
                {
                    num++;
                }
                if (num > length)
                {
                    break;
                }
                builder.Append(demand.Substring(i, 1));
            }
            builder.Append(substitute);
            return builder.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="demand"></param>
        /// <returns></returns>
        public static int SubStringLength(string demand)
        {
            if (string.IsNullOrEmpty(demand))
            {
                return 0;
            }
            ASCIIEncoding encoding = new ASCIIEncoding();
            int num = 0;
            byte[] bytes = encoding.GetBytes(demand);
            for (int i = 0; i < bytes.Length; i++)
            {
                if (bytes[i] == 0x3f)
                {
                    num += 2;
                }
                else
                {
                    num++;
                }
            }
            return num;
        }

        public static string HtmlDecode(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                value = value.Replace("<br>", "\n");
                value = value.Replace("<br/>", "\n");
                value = value.Replace("<br />", "\n");
                value = value.Replace("&gt;", ">");
                value = value.Replace("%lt;", "<");
                value = value.Replace("&nbsp;", " ");
                value = value.Replace("&#39;", "'");
                value = value.Replace("&quot;", "\"");
            }

            return value;
        }

        /// <summary>
        /// 将字符串转成Html
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string HtmlEncode(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Replace("<", "&lt;");
                str = str.Replace(">", "&gt;");
                str = str.Replace(" ", "&nbsp;");
                str = str.Replace("'", "&#39;");
                str = str.Replace("\"", "&quot;");
                str = str.Replace("\r\n", "<br />");
                str = str.Replace("\n", "<br />");
            }

            return str;
        }

        /// <summary></summary>
        /// <param name="url"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string UrlToString(string url, ref int index)
        {
            Regex reg = new Regex(@"^\S*_\d*$", RegexOptions.IgnoreCase);

            if (reg.IsMatch(url))
            {
                int i = url.LastIndexOf('_');
                index = Convert.ToInt32(url.Substring(i + 1));
            }

            url = url.Replace("-", " ");
            //url = url.Replace("  ", "-");
            url = HtmlEncode(url);

            return url;
        }

        /// <summary></summary>
        /// <param name="str"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string StringToUrl(string str, int index)
        {
            str = HtmlDecode(str);

            if (str.Contains("-"))
                str += "_" + index;

            if (str.Contains("/"))  //含有这个符号的替换掉
                str = str.Replace("/", "_") + "_" + index;

            //str = str.Replace("-", "--");
            str = str.Replace(" ", "-");

            return str;
        }
    }
}
